<?PHP


require_once WACT_ROOT.'controller/controller.inc.php';

require_once APP_ROOT.'view/pub/layoutright.view.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/league.php';
require_once APP_ROOT.'model/common/price.php';
require_once APP_ROOT.'model/common/date.php';
require_once APP_ROOT . "model/common/site.php";



class LeaguesDetailView extends LayoutRightView {
	function LeaguesDetailView($TemplateFile){
		parent::LayoutRightView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare(&$controller, &$request, &$responseModel);

		if(!Session::isLogin())
			$this->Template->set('login',' ');

		$league_id=$request->getParameter('id');

		$this->Template->set('league_id',$league_id);

		$league=&League::getRecord($league_id);

		if(Session::isLogin()&&Session::getUserId()==$league->get('user_id'))
			$this->Template->set('author',' ');

        
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Camp/League profile - '.$league->get('name'));


 

		$datestr=$league->get('date_added');
		$mydate=strtotime($datestr);
        $tz =& new MyTimeZone();
        $mydate = $tz->timeGMT($mydate);
		$datestr = gmstrftime('%b %d, %Y',$mydate);
		$this->Template->set('date_added',$datestr);

		$prem=$league->get('premium');
		if($prem&&$prem==0) $prem=null;
		$this->Template->set('prem',$prem);

		$this->Template->set('name',$league->get('name'));



		$event=&League::getEventName($league->get('event'));
		$this->Template->set('event',$event);

		$category1=$league->get('category1');
		$category2=$league->get('category2');

		//categories
		if($category1) {
			$category=$category1;
			if($category2)	$category.='/'.$category2;
			$this->Template->set('category',$category);
		} else if($category2){
			$this->Template->set('category',$category2);
		}



		//school
		$org=$league->get('org_title');
		if($org)$this->Template->set('org_title',$org);

		$location=null;
		$saddress=$league->get('org_address');
		$scity=$league->get('org_city');
		$sstate=$league->get('org_state');
		$szip=$league->get('org_zip');
		$sphone=$league->get('org_phone');
		$semail=$league->get('org_email');
		if($saddress) {
			$this->Template->set('org_address',$saddress);
			$location=' ';
		}
		if($scity) $location=$scity;
		if($sstate&&$sstate!='(N') $location.=', '.$sstate;
		if($szip) $location.=' '.$szip;
		if($location&&$location!=' ')
			$this->Template->set('org_lastaddress',$location);
		if($sphone) {
			$location='Phone: '.$sphone;
			$this->Template->set('org_phone',$location);
		}
		if($semail) {
			$location='Email: '.$semail;
			$this->Template->set('org_email',$location);
		}
		if($location)$this->Template->set('org_location',true);

		$other=$league->get('additional');
		if($other) $this->Template->set('other',$other);

		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

	}

}



class LeaguesTeaserView extends LayoutRightView {
	function LeaguesTeaserView($TemplateFile){
		parent::LayoutRightView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare(&$controller, &$request, &$responseModel);
		if(!Session::isLogin())
			$this->Template->set('login',' ');

		$league_id=$request->getParameter('id');

		$this->Template->set('league_id',$league_id);

		$league=&league::getRecord($league_id);

		if(Session::isLogin()&&Session::getUserId()==$league->get('user_id'))
			$this->Template->set('author',' ');

               
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - league profile - '.$league->get('name').' - Please subscribe');

 

		$datestr=$league->get('date_added');
		$mydate=strtotime($datestr);
        $tz =& new MyTimeZone();
        $mydate = $tz->timeGMT($mydate);
		$datestr=strftime('%b %d, %Y',$mydate);
		$this->Template->set('date_added',$datestr);

		$this->Template->set('prem',true);

		$this->Template->set('name',$league->get('name'));

		$org=$league->get('org_title');
		if($org)$this->Template->set('org_title',$org);

		$event=&League::getEventName($league->get('event'));
		$this->Template->set('event',$event);

		$price =& Price::getPrice(4);
		$price1 =& Price::getPrice(1);
		$price2 =& Price::getPrice(2);
		$price3 =& Price::getPrice(3);

		$price = number_format($price,2);
		$price1 = number_format($price1,2);
		$price2 = number_format($price2,2);
		$price3 = number_format($price3,2);
		$this->Template->set('price',$price);
		$this->Template->set('price1',$price1);
		$this->Template->set('price2',$price2);
		$this->Template->set('price3',$price3);


			//price per year
		//$price=&Price::getPriceByType(4);
		//$number=$price->get('price');
		//list($int,$dec)=explode('.',$number);
		//if(!($dec>0))
			//$this->Template->set('price',$int);

		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');
	}

}


class LeaguesDetailPage extends PageController{
	function leaguesDetailPage(){
		parent::PageController();

		$showdetail=false;


		$league_id=Request::getParameter('id');
		$leagues=&League::getRecord($league_id);

		if((!$leagues->get('premium'))||($leagues->get('premium')==0))
			$showdetail=true;

		else if(Session::isLogin()){
			$user_id=Session::getUserId();
			if(User::isUserSubscriptionActive($user_id)) {
				$showdetail=true;
			}
			else if(User::isUserAdmin($user_id)){
				$showdetail=true;
			}

			if($leagues->get('user_id')==$user_id)
					$showdetail=true;
		}

         
        $mytemplate1 = 'pub/leagues/detail.html';
        $mytemplate2 = 'pub/leagues/teaser.html';
         
		if($showdetail)
			$this->setDefaultView(new LeaguesDetailView($mytemplate1));
		else
			$this->setDefaultView(new LeaguesTeaserView($mytemplate2));
	}


}

?>
